package com.room.controller.dorm;

import com.room.pojo.Apartment;
import com.room.pojo.Classes;
import com.room.pojo.DormResult;
import com.room.pojo.Student;
import com.room.service.ApartmentService;
import com.room.service.ClassesService;
import com.room.service.CollegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @创建人 lin
 * @创建时间 2019/10/6
 * @描述
 */
@RestController
@RequestMapping("/dorm/manage")
public class ApartmentController {
    @Autowired
    ApartmentService apartmentService;

    @Autowired
    CollegeService collegeService;

    @Autowired
    ClassesService classesService;


    /**
     * 添加公寓
     * @param apartment
     * @return
     */
    @RequestMapping(value = "/dorm",method = RequestMethod.POST)
    public DormResult addApartment(Apartment apartment){
        String maxNo = null;
                if(apartmentService.getMaxId()==null){
                maxNo = "1";
            }else{
              maxNo = apartmentService.getMaxId();
            int a = Integer.parseInt(maxNo);
            ++a;
            maxNo = String.valueOf(a);
        }
                apartment.setApartmentId(maxNo);
                if(apartmentService.inserApartment(apartment)==1){
                    return DormResult.ok("添加成功");
                }
                return DormResult.error("添加失败");
    }

    /**
     * 修改公寓
     * @param apartment
     * @return
     */
    @RequestMapping(value = "/dorm",method = RequestMethod.PUT)
    public DormResult updateApartment(Apartment apartment){
        if(apartmentService.updateApartment(apartment)==1){
            return DormResult.ok("修改成功");
        }
        return DormResult.error("修改失败");
    }

    /**
     * 删除指定id的公寓
     * @param ids
     * @return
     */
    @RequestMapping(value = "/dorm/{ids}",method = RequestMethod.DELETE)
    public DormResult deleteById(@PathVariable String ids){
        if(apartmentService.deleteApartById(ids)){
            return DormResult.ok("删除成功");
        }
        return DormResult.error("删除失败");
    }


    /**
     * 根据分页参数返回公寓数据
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/dorm",method = RequestMethod.GET)
    public Map<String,Object> getAnnounByPage(
            @RequestParam(defaultValue = "1")Integer page,
            @RequestParam(defaultValue = "6")Integer size
    ){
        Map<String,Object> map = new HashMap<>();
        List<Apartment> apartments = apartmentService.getApartmentByPage(page, size);
        Long total = apartmentService.getApartmentCount();
        map.put("apartments",apartments);
        map.put("total",total);
        return map;
    }



    @RequestMapping(value = "getInitApartInit")
    public List<Apartment> getAllApartment(){
       return apartmentService.getAllApartment();
    }



}
